package com.clickpaas.materialsprocurement.dao.Impl;

import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.codegen.SupplierinformationApi;
import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.model.*;
import com.clickpaas.materialsprocurement.bean.MateralsDTOBean;
import com.clickpaas.materialsprocurement.config.AutoLoadConfig;
import com.clickpaas.materialsprocurement.constant.ConfConstant;
import com.clickpaas.materialsprocurement.dao.SupplierInformationDao;
import com.clickpaas.materialsprocurement.entity.PageInfoEntity;
import com.clickpaas.materialsprocurement.entity.SupplierInformationEntity;
import com.clickpaas.materialsprocurement.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Repository
public class SupplierInformationDaoImpl implements SupplierInformationDao {

    @Resource
    private MateralsDTOBean materalsDtoBean;
    @Resource
    private SupplierinformationApi supplierinformationApi;

    private List<SupplierInformationDTOResponse> querySupplierInfo(SupplierInformationDTO supplierInformationDTO){
        DataResponseListSupplierInformationDTO supplierInformationUsingPOST = supplierinformationApi.findSupplierInformationUsingPOST(supplierInformationDTO);
        if(null == supplierInformationUsingPOST
                || null == supplierInformationUsingPOST.getData()
                || supplierInformationUsingPOST.getData().size()<=0
                || !"SUCCESS".equals(supplierInformationUsingPOST.getMessage())){
            log.info("当前查询的数据：【{}】",supplierInformationUsingPOST);
            return null;
        }
        return supplierInformationUsingPOST.getData();
    }

    /**
     * 根据条件查询供应商资料对象对象数据
     *
     * @param supplierInformationEntity
     */
    @Override
    public List<SupplierInformationDTOResponse> getSupplierInfoByCondtion(SupplierInformationEntity supplierInformationEntity) {
        SupplierInformationDTO supplierInformationDTO1 = materalsDtoBean.supplierInformationDTO();
        try {
            SpringUtil.copyPropertiesIgnoreNull(supplierInformationEntity,supplierInformationDTO1);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",supplierInformationEntity);
        }
        return querySupplierInfo(supplierInformationDTO1);
    }

    /**
     * 查询全部供应商资料对象数据
     */
    @Override
    public List<SupplierInformationDTOPageInfoResponse> getSupplierInfoByCondtion() {
        PageInfoEntity pageInfoEntity = new PageInfoEntity();
        pageInfoEntity.setPageNum(ConfConstant._PAGE_NUM);
        pageInfoEntity.setPageSize(ConfConstant._PAGE_SIZE);
        /**
         * 分页查询
         */
        PageInfoEntity tagetPageInfoEntity = getTotal(pageInfoEntity);
        if (null != tagetPageInfoEntity) {
            List<SupplierInformationDTOPageInfoResponse> listAll = new ArrayList<>();
            for (int i = 1; i <= tagetPageInfoEntity.getRound(); i++) {
                DataResponseSupplierInformationDTOPageInfodata dataInfo = querySupplierByPageInfo(i, tagetPageInfoEntity.getPageSize());
                if (null != dataInfo && dataInfo.getDataList().size()>0){
                    listAll.addAll(dataInfo.getDataList());
                }
            }
            if (listAll.size() > 0) {
                return listAll;
            }
        }
        return null;
    }

    /**
     * 获取分页查询数据
     * @return
     */
    private PageInfoEntity getTotal(PageInfoEntity pageInfoEntity){
        Integer pageNum = pageInfoEntity.getPageNum();
        Integer pageSize = pageInfoEntity.getPageSize();
        DataResponseSupplierInformationDTOPageInfodata dataResponseSupplierInformationDTOPageInfodata = querySupplierByPageInfo(pageNum, pageSize);
        if (null != dataResponseSupplierInformationDTOPageInfodata
                && StringUtils.isNotBlank(dataResponseSupplierInformationDTOPageInfodata.getTotal())){
            /**
             * 查询总数
             */
            String total = dataResponseSupplierInformationDTOPageInfodata.getTotal();
            double ceil = Math.ceil(Double.valueOf(total) / Double.valueOf(AutoLoadConfig.PAGE_SIZE));
            /**
             * 分页查询次数
             */
            Long round = Math.round(ceil);

            pageInfoEntity.setTotal(Integer.valueOf(total));
            pageInfoEntity.setPageSize(AutoLoadConfig.PAGE_SIZE);
            pageInfoEntity.setRound(round.intValue());

            return pageInfoEntity;
        }
        return null;
    }


    /**
     * 分页查询物料数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    public DataResponseSupplierInformationDTOPageInfodata querySupplierByPageInfo(Integer pageNum, Integer pageSize) {
        SupplierInformationDTOPageInfo supplierInformationDTOPageInfo = materalsDtoBean.supplierInformationDTOPageInfo();
        supplierInformationDTOPageInfo.set$PageNum(pageNum);
        supplierInformationDTOPageInfo.set$PageSize(pageSize);
        DataResponseSupplierInformationDTOPageInfo pageInfoSupplierInformationUsingPOST = supplierinformationApi.findPageInfoSupplierInformationUsingPOST(supplierInformationDTOPageInfo);
        if (null == pageInfoSupplierInformationUsingPOST || null == pageInfoSupplierInformationUsingPOST.getData())return null;
        return pageInfoSupplierInformationUsingPOST.getData();
    }
}
